<?

//print_r($_POST);

$cmbHousingType  = $HTTP_POST_VARS['cmbHousingType'];
$cmbIncomeStatus  = $HTTP_POST_VARS['cmbIncomeStatus'];
$visitID  = $HTTP_POST_VARS['visitID'];

$txtCurrentAddress = $HTTP_POST_VARS['txtCurrentAddress'];
$txtCurrentPostCode  = $HTTP_POST_VARS['txtCurrentPostCode'];
$txtCurrentState  = $HTTP_POST_VARS['txtCurrentState'];
$txtCurrentSuburb = $HTTP_POST_VARS['txtCurrentSuburb'];
$optErGiven = $HTTP_POST_VARS['optErGiven'];
$txtInterviewer = $HTTP_POST_VARS['txtInterviewer'];

$txtVisitDate = $HTTP_POST_VARS['txtVisitDate'];
					$DbDOB=explode("/",$txtVisitDate);
					$dayDOB=$DbDOB[0];
					$monthDOB=$DbDOB[1];
					$yearDOB=$DbDOB[2];
					$txtVisitDate=$yearDOB."-".$monthDOB."-".$dayDOB;

$txtChildren = $HTTP_POST_VARS['txtChildren'];
$txtAdults  = $HTTP_POST_VARS['txtAdults'];
$optClientSignature = $HTTP_POST_VARS['optClientSignature'];
$txtOtherHousingType  = $HTTP_POST_VARS['txtOtherHousingType'];
$txtOtherIncomeStatusType  = $HTTP_POST_VARS['txtOtherIncomeStatusType'];
$txtOtherEr  = $HTTP_POST_VARS['txtOtherEr'];
$txtOtherIncomeSourceType = $HTTP_POST_VARS['txtOtherIncomeSourceType'];
$txtFortnightlyPayments  = $HTTP_POST_VARS['txtFortnightlyPayments'];
$txtPhone = $HTTP_POST_VARS['txtPhone'];
$txtMobile = $HTTP_POST_VARS['txtMobile'];
$txtCentreLinkAmount = $HTTP_POST_VARS['txtCentreLinkAmount'];
$cmbSite = $HTTP_POST_VARS['cmbSite'];
$cmbAgency  = $HTTP_POST_VARS['cmbAgency'];
$txtOtherPaymentType = $HTTP_POST_VARS['txtOtherPaymentType'];
$extraFoodParcels = $HTTP_POST_VARS['extraFoodParcels'];
$txtChequePayee = $HTTP_POST_VARS['txtChequePayee'];
$txtErAmountRecieptNo = $HTTP_POST_VARS['txtErAmountRecieptNo'];
$txtReasonChequePayment = $HTTP_POST_VARS['txtReasonChequePayment'];
$txtVisitRecordedDate  = '';
$referred_internal = $HTTP_POST_VARS['referred_internal'];
$referred_external = $HTTP_POST_VARS['referred_external'];
$txtReturnDate = $HTTP_POST_VARS['txtReturnDate'];
$txtActionOther = $HTTP_POST_VARS['txtActionOther'];

//=====================CLIENT INCOMES=============================

$amount = $HTTP_POST_VARS['amount'];
$whenDue = $HTTP_POST_VARS['whenDue'];
$incomes = $HTTP_POST_VARS['incomes'];

deleteFromTable("er_client_income","visit_id",$visitID);

$whenDueAmerican = array();
$arrWhenDueEnglish = array();
for($i=0;$i<count($whenDue);$i++)
{
	if (!empty($whenDue[$i])) {
		$whenDueAmerican=explode("/",$whenDue[$i]);
		$day=$whenDueAmerican[0];
		$month=$whenDueAmerican[1];
		$year=$whenDueAmerican[2];
		$whenDueEnglish=date("Y-m-d",mktime(0,0,0,$month,$day,$year));
		array_push($arrWhenDueEnglish, $whenDueEnglish);
	}
}

insert_incomes($incomes,$visitID, $amount, $arrWhenDueEnglish);

function insert_incomes($incomes, $visitID, $amount, $whenDue)
{
	$query = create_checkbox_query($incomes, "er_client_income", $visitID, $amount, $whenDue);
}

function create_checkbox_query($arrIncomes, $table, $visitId, $arrIncomeAmount, $arrWhenDue) {
   $i = 0;
   foreach ($arrIncomes as $value)
   {
   	$q = "INSERT INTO $table (income_id,visit_id,income_amount,when_due) VALUES";

   	$incomeId = getIdFromListBox("er_income_source","income_type",$value);
	if (!empty($incomeId) && $incomeId != 1) {
	     $q .=  " ( $incomeId ,$visitId, $arrIncomeAmount[$i], '$arrWhenDue[$i]')" ;
		 //echo $q."<br />";
	     $i++;
	     r_db($q);
	 }

   }
   return $q;
}




//====================PAYMENT ON VISIT========================================



$payment = $HTTP_POST_VARS['payment'];
$txtReasonChequePayment = $HTTP_POST_VARS['txtReasonChequePayment'];
$extraFoodParcels = $HTTP_POST_VARS['extraFoodParcels'];
$txtChequePayee = $HTTP_POST_VARS['txtChequePayee'];
$txtOtherPaymentType = $HTTP_POST_VARS['txtOtherPaymentType'];
$txtErRecieptNo = $HTTP_POST_VARS['txtErAmountRecieptNo'];

$queryPayment = "SELECT * FROM er_payment";
$resultPayment = r_db($queryPayment);

$i=0;

//deletes all visits from er_payment_on_visit
$q = "DELETE FROM er_Payment_On_Visit WHERE visit_id=$visitID";
r_db($q);

while ($rowPayment = mysql_fetch_array($resultPayment))
{
	if (!empty($_POST['payment'.$rowPayment[0]])) {
		$q = "INSERT INTO er_payment_on_visit (payment_id, visit_id, payment_amount) VALUES ($rowPayment[0], $visitID, {$_POST['payment'.$rowPayment[0]]})";
		r_db($q);
	}
	$i++;
}



//=====================INSERTION IN VISIT TABLE========================

$housingId = getIdFromListBox("er_housing_type","Housing_Type",$cmbHousingType);
$incomeStatusId = getIdFromListBox("er_income_status","Income_Status_Type",$cmbIncomeStatus);
$agencyId = getIdFromListBox("er_agency","agency_name",$cmbAgency);

$queryVisit = "

					UPDATE er_visit SET
					visit_recorded_date='$txtVisitRecordedDate',
					visit_date='$txtVisitDate',
					address='$txtCurrentAddress',
					postcode=$txtCurrentPostCode,
					state='$txtCurrentState',
					suburb='$txtCurrentSuburb',
					er_given='$optErGiven',
					interviewer='$txtInterviewer',
					no_children_assistted=$txtChildren,
					no_adults_assistted=$txtAdults,
					site_visited_id=$cmbSite,
					client_signature='$optClientSignature',
					housing_type_id=$housingId,
					other_housing_type='$txtOtherHousingType',
					income_status_id=$incomeStatusId,
					other_income_status='$txtOtherIncomeStatusType',
					other_ER='$txtOtherEr',
					action_taken='$txtActionOther',
					other_income_source='$txtOtherIncomeSourceType',
					agency_id=$agencyId,
					rent_paid_pf=$txtFortnightlyPayments,
					phone_no='$txtPhone',
					mobile_no='$txtMobile',
					centrelink_loan_amt_borrowed=$txtCentreLinkAmount,
					other_payment_type = '$txtOtherPaymentType',
					extra_food_parcels = '$extraFoodParcels',
					cheque_payee = '$txtChequePayee',
					er_amount_receipt_no = '$txtErAmountRecieptNo',
					reason_cheque_payment = '$txtReasonChequePayment',
					referred_internal = '$referred_internal',
					referred_external = '$referred_external',
					return_date = '$txtReturnDate'
					WHERE visit_id=$visitID
			";
//echo $queryVisit;
$resultVisit = r_db($queryVisit);


//===========================RELIEF ON VISIT============================================

$emergencyReliefs = $HTTP_POST_VARS['emergencyReliefs'];
update_reliefs($emergencyReliefs,$visitID);

//============================ACTION TAKEN========================

$actionTaken = $HTTP_POST_VARS['actionTaken'];
update_action($actionTaken,$visitID);


//successfullUpdation($visitID);
$greetingdate=$HTTP_POST_VARS['txtVisitDate'];
echo "<b>Record of visit made on $greetingdate has been successfully updated!</b>";
echo "<br />If the visit is within the last 12 months it will display below.  Please check to ensure the visit has saved correctly.";

$queryPayment = "SELECT client_id FROM er_visit WHERE visit_id = $visitID";
$queryPayment = r_db($queryPayment);
$queryPayment = mysql_fetch_array($queryPayment);
$ClientID = $queryPayment[0];


$visit_id = $visitID;
$client_id = $ClientID;


include_once("./pages/page_2_modify.php");


//FUNCTIONS START *********************************************************************
function update_reliefs($emergencyReliefs, $visitID)
{
	deleteFromTable("er_relief_on_visit", "visit_id",$visitID);
	$query = create_er_checkbox_query($emergencyReliefs, "er_relief_on_visit", $visitID);
	r_db($query);
}

function update_action($actionTaken, $visitID)
{
	deleteFromTable("er_action_on_visit", "visit_id",$visitID);
	$query = create_action_checkbox_query($actionTaken, "er_action_on_visit", $visitID);
	r_db($query);
}


function create_action_checkbox_query($arrActionTaken, $table, $visitID) {
	$q = "INSERT INTO $table (visit_id,action_id) VALUES";
	$i = 0;
	foreach ($arrActionTaken as $check)
	{
		$q .=  " ($visitID,$check)" . ",";
		$i++;
	}
	return substr($q, 0, -1);
}

function create_er_checkbox_query($arrEmergencyReliefs, $table, $visitID) {
	$q = "INSERT INTO $table (visit_id,relief_id) VALUES";
	$i = 0;
	foreach ($arrEmergencyReliefs as $check)
	{
		$q .=  " ($visitID,$check)" . ",";
		$i++;
	}
	return substr($q, 0, -1);
}

?>

